<?php
include_once 'snc-core/View.php';
include_once 'snc-core/ControllerDispatcher.php';
include_once 'snc-core/Paths.php';
/**
 * call combine view + controller of all active module and main content
 * and put them to position in template. * 
 * @author snc
 *
 */
class Template {
	private $context;	
	private $mContent;
	private $tpl;
	function __construct($context){
		$this->context  = $context;
		//get content of requested Url
		$this->main();
		
		//load template	
		if($this->isOn()){		
			$this->loadTemplate();
		}else{			
			$this->showMain();
		}
	}	
	
	/**
	 * load template	 
	 */
	private function loadTemplate(){
		//display user template
		if(!$this->isAdmin()){
			$tplName = $this->context["CONFIG"]["basic"]["template"]["name"];
			// path to index.php of an template
			$index = APPLICATION_PATH.TEMPLATE_PATH.'/'.$tplName
								.TEMPLATE_INDEX;
			
			if(!file_exists($index)){
				echo "no template was found";	
                                echo $index;
			}else{		
				$template =  HTTP.$_SERVER['HTTP_HOST'].'/templates/'.$tplName;										                              
				include_once $index;									
			}
		}else{
			//display admin template
			//path to index 
			$index = APPLICATION_PATH."/admins/template/html/index.php";			
			if(!file_exists($index)){
				echo "no template was found";	
                                echo $index;
			}else{		
				$template =  HTTP.$_SERVER['HTTP_HOST'].'/admins/template';                                 
				include_once $index;				
			}
		}
	}
	
	/**
	 * get Content for dedicated url
	 */
	public function main(){
		$this->mContent = $this->getContent($this->context["REQUEST_URI"]);                
	}
	
	/**
	 * this method will show maincontent to layout
	 */
	public function showMain(){
		echo $this->mContent;
	}
	
	/**
	 * get content of all position and put it to global position
	 * return content of all module which are active at position.
	 */
	public function pos($position){
		$content = null;
		$listPos = $this->getModulesOfEachPos();
		if(array_key_exists($position, $listPos)){			
        	$listModule = $listPos[$position];
        	//show content of all moudle in this position
			foreach ($listModule as $module){				
				$content .= $this->getContent($module);				 
			}
			//show content to screen
			echo $content;
		}//TODO : implement error site here, if no position was found      
				
	}
	
	/**
	 * get content of module
	 * @return Module's content	 
	 */
	public function getContent($uri){
		//dispatch uri
		$ctrDispatcher = new ControllerDispatcher($uri);		
		$mvc = $ctrDispatcher->validate();	
		//get view
		$view = new View($mvc,$this->context);
		//get view content		
		$content = $view->getContent();
		$this->tpl = $view->isTplOn();
		//get template status		
		return $content;
	}
	
	/**
	 * test that request from admin or from normal user
	 * @return boolean
	 */
	public function isAdmin(){
		$ctrDispatcher = new ControllerDispatcher($this->context["REQUEST_URI"]);	
		$ctrDispatcher->validate();	
		return $ctrDispatcher->isAdmin();
	}
	
	/**
	 * Get all active Module from config array.	 * 
	 */
	private function getActivedModules(){
		$listModule = $this->context["CONFIG"]["basic"]["module"];
		$actModules = array();
		//new way to go through all elements in array
		// while (current elemnt of array still exists)
		// can get the key of this element
		// then next(array) set next element of this array to continue the while
		while($module = current($listModule)){
			$moduleName = key($listModule);
			if($module[0] == 1){
				$actModules[] = array ("name"=>$moduleName,
									   "position"=>$module[1]);
			}
			next($listModule); //return next element of listmodule
		}
		return $actModules ;
	}
	
	/**
	 * this method will create a list of positions and their modules, to which
	 * they belong
	 * @return array(position=>array(module1,module2...))
	 */
	public function getModulesOfEachPos(){
		//list of combination(modulename,position)
		$listModPos = $this->getActivedModules();
		$positions = array();
		//create a list of positions without duplication
		foreach ($listModPos as $entryModPos) {
			if(!in_array($entryModPos["position"],$positions)){
				$positions[] = $entryModPos["position"];
			}
		}
		//create a list of position with its modules.		
		$listPosMod = array();
		
		foreach ($positions as $position){
			$listModule = array();
			foreach($listModPos as $entryModPos){		
				//collect module and add to each position, to which module belong. 
				if($entryModPos["position"] == $position){
					  $listModule[] = "/".$entryModPos["name"];
				}
			}
			$listPosMod[$position] = $listModule;
		}
		return $listPosMod;		
	}	
	
	/**
	 * call this request a step before to know template is on or off
	 * @return boolean status of template
	 */
	public function isOn(){
		return $this->tpl;
	}
}